n=19

##快慢指针求环的思想，求解该问题

def bitSquareSum(n):
    sum=0
    while n>0:
        sum+=(n%10)*(n%10)
        n=int(n/10)
    return sum
def isHappy(n):
     if n==1:
         return True
     slow,fast=n,n
     slow=bitSquareSum(slow)
     fast=bitSquareSum(fast)
     fast=bitSquareSum(fast)
     while fast!=slow:
         slow=bitSquareSum(slow)
         fast=bitSquareSum(fast)
         fast=bitSquareSum(fast)
     return slow==1


print(isHappy(n))